#coding:utf8
from config import *
from routin import *
from gui import *
from gui2 import *
from mpp import *
from keyr import * 
from bk_jump import *
def ini_mid_suo():
    a = get_snap_sort()
    if len(a)==0:
        return
    p1 = (a[-1][1],a[-1][2])
    if len(a)>1:
        p2 = (a[-2][1],a[-2][2])
    else:
        p2 = getp()
    cfg.suo_middle_du =  math.atan2(p1[1]-p2[1],p1[0]-p2[0])
    cfg.suo_middle_to = cfg.to
    cfg.suo_middle_pos = getp()
    cfg.suo_middle_start_pos = get_cur_pos()
    cfg.is_suo_middle = True        
    cfg.suo_middle_to_scal = True
    cfg.suo_middle_exit_co = 1
    
def deal_mid_suo():
    x,y = get_cur_pos()
    dx = x-cfg.suo_middle_start_pos[0]
    dy = y-cfg.suo_middle_start_pos[1]
    l = math.sqrt(dx*dx+dy*dy)
    du = math.atan2(dy,dx)
    d = cfg.suo_middle_du - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    dd = math.cos(d) *l *get_mid_suo_scale()
    to_lock.acquire() 
    cfg.to = cut_to(cfg.suo_middle_to + dd)                          
    set_to_chg()           
    to_lock.release()
def ini_suo():
    print('ini_suo')
    cfg.is_suo = True
    if cfg.w2showing:
        cfg.suo_dis = max(1, cal_dis(get_cur_pos() , cfg.fix_pos))
        cfg.suo_to = cfg.to
    else:
        clear(True)
        cfg.suo_to = 0
        cfg.show_one_on = True
        cfg.show_one_onetime = True
        cfg.suo_dis = 1
def deal_suo():
    d =  cal_dis(get_cur_pos(),cfg.fix_pos)- cfg.suo_dis
    to_lock.acquire()
    if d >= 0:
        cfg.to = cut_to(cfg.suo_to + d * get_suo_di())            
    else:
        cfg.to = cfg.suo_to * cal_dis(get_cur_pos(),cfg.fix_pos) / cfg.suo_dis
        cfg.min_ge = 0
    
    set_to_chg()           
    to_lock.release()
    if not cfg.w2showing:
        dotdot()

def suo2():
    if cfg.middle_pressed and not cfg.middle_pressed_other and not cfg.x2_pressed and not pm.scroll_pressed and \
            not cfg.x2_mode and cfg.w2showing:
        ini_suo()
        cfg.middle_pressed_other = True
    # if cfg.x2_pressed and not cfg.is_suo and not cfg.x2_pressed_then_other and not cfg.w2showing :
    #     cfg.x2_pressed_then_other = True
    #     if cfg.mouse_tip_no_show and cfg.x2_type==0:
    #         pass
    #     else:
    #         print('x2 ini suo',cfg.x2_type)
    #         ini_suo()
    elif cfg.x2_pressed and not cfg.is_suo_middle and not cfg.x2_pressed_then_other and cfg.x2_type==0 and cfg.w2showing:
        cfg.x2_pressed_then_other = True
        ini_mid_suo()
    if cfg.is_suo_middle:
        deal_mid_suo()
    if (cfg.middle_pressed or cfg.x2_pressed) and cfg.is_suo:
        deal_suo()

def on_move(x,y):
    ct = time.time()
    if cfg.jump_state!=0 and (cfg.middle_pressed or cfg.x2_pressed) and not is_jump_base():        
        jump_state_chg(0)
        cfg.middle_pressed_other=True
        cfg.x2_pressed_then_other=True
    cfg.cur_screen = get_screen_indx(x,y)
    cfg.last_active_time = ct
    cfg.zi_no_color = 0
    if ct-cfg.x2_mode_exit_time>0.01:
        cfg.x2_mode_exit_time = 0        
    clear_add_show()          

    if cfg.x2_pressed and cfg.x2_type==1 and cfg.x2_pressed_then_other and cfg.w2showing and cfg.x2_mode:
        cfg.has_update = True
    if cfg.x2_pressed and cfg.x2_type==1 and not cfg.x2_pressed_then_other and \
          cfg.w2showing and not cfg.x2_mode and not cfg.is_suo and not cfg.is_suo_middle:
        cfg.x2_pressed_then_other = True
        into_x2_mode()
        cfg.has_update = True
        cfg.sum = str(cfg.far[0])
        deal_x2_mode_num()
    if not cfg.x2_mode:
        suo2()        
        
    cfg.last_move_time = ct
    
    
    if float(cfg.sum) != 0  and ct-cfg.key_input_time>0.1 and not\
          (cfg.x2_mode) and not (cfg.disable_auto_jump and not cfg.w2showing):
        x, y = getp(cfg.last_mouse_pos, False)
        sx, sy = cfg.fix_pos
        dx = x - sx
        dy = y - sy
        cfg.last_m = (dx,dy)
        cfg.last_m_time = ct
        cfg.last_sum =float(cfg.sum) 
        dx = round(dx*float(cfg.sum) + dx)
        dy = round(dy*float(cfg.sum) + dy)
        
        cfg.move_task = (sx + dx - cfg.last_mouse_pos[0], sy + dy - cfg.last_mouse_pos[1], 1)
        cfg.move_task_time = ct
        print('move_task', cfg.move_task)
        cfg.isShow = False
        clear(False)                          
   
def on_click(x, y, button, pressed):
    on_click2(x, y, button, pressed,False)
def is_right():
    if not cfg.offMid:
        return False
    t = time.time()
    while True:
        if abs(t-cfg.left_mid_time)<0.01:
            return True
        if time.time()-t>0.005:
            return False
        time.sleep(0.001)

def is_left_rightBtn():
    t = time.time()
    while True:
        if abs(t-cfg.left_right_time)<0.01:
            return True
        if time.time()-t>0.005:
            return False
        time.sleep(0.001)

def is_left_leftBtn():
    t = time.time()
    while True:
        if abs(t-cfg.left_left_time)<0.01:
            return True
        if time.time()-t>0.005:
            return False
        time.sleep(0.001)

def is_left_sc():
    t = time.time()
    while True:
        if abs(t-cfg.left_sc_time)<0.01:
            return True
        if time.time()-t>0.005:
            return False
        time.sleep(0.001)

def on_right(pressed):
    if pressed:
        ctrl2_fire()
        if cfg.x2_pressed:
            cfg.x2_pressed_then_other=True
            cfg.ctrl2_other=True
            no_to_3()
        if cfg.middle_pressed:
            up_double()
            cfg.middle_pressed_other=True
            cfg.ctrl2_other=True
    else:
        if cfg.ctrl2_job and time.time()-cfg.ctrl2_time<x_cancel_gap and not cfg.ctrl2_other:
            do_ctrl2()
        cfg.ctrl2_job = False

req1_co1=0 
req1_co2=0
req2_co1=0
req2_co2=0 
req3_co1=0
req3_co2=0 

rsp1_co1=0
rsp1_co2=0 
rsp2_co1=0 
rsp2_co2=0 
rsp3_co1=0 
rsp3_co2=0 

def filterF():
    global rsp1_co1,rsp1_co2,rsp2_co1,rsp2_co2,rsp3_co1,rsp3_co2
    while True:
        time.sleep(0.01)
        if rsp1_co1<req1_co1 and rsp1_co1==rsp1_co2:
            time.sleep(0.01)
            on_click2(0,0,'x1',True,True)
            # on_right(True)
            rsp1_co1+=1
        
        if rsp1_co2<req1_co2 and rsp1_co2<rsp1_co1:
            time.sleep(0.01)
            on_click2(0,0,'x1',False,True)
            # on_right(False)
            rsp1_co2+=1

        if rsp2_co1<req2_co1 and rsp2_co1==rsp2_co2:
            time.sleep(0.01)
            deal_mid(True)
            rsp2_co1+=1

        if rsp2_co2<req2_co2 and rsp2_co2<rsp2_co1:
            time.sleep(0.01)
            deal_mid(False)
            rsp2_co2+=1

        if rsp3_co1<req3_co1 and rsp3_co1==rsp3_co2:
            time.sleep(0.01)
            if not cfg.is_pressed or cfg.w2showing or cfg.min_ge!=0:
                on_click2(0,0,'x2',True,True)
            rsp3_co1+=1

        if rsp3_co2<req3_co2 and rsp3_co2<rsp3_co1:
            time.sleep(0.01)
            if not cfg.is_pressed  or cfg.w2showing or cfg.min_ge!=0:
                on_click2(0,0,'x2',False,True)
            else:
                deal_pressed()
            rsp3_co2+=1
sup_co=0  
if __name__ == '__main__':        
    filterFThr = threading.Thread(target=filterF)
    filterFThr.daemon = True
    filterFThr.start()

    bkThr = threading.Thread(target=jump_bg_thr)
    bkThr.daemon = True
    bkThr.start()

    def win32_event_filter(msg, data):    
        global req1_co1,req1_co2,req2_co1,req2_co2,req3_co1,req3_co2
        st =str(msg)
        ct = time.time()
        if st=='512':
            if do_suppress():
                lis.suppress_event()
                return      
            if jump_state_sup2()  :
                if is_system_move(ct):
                    return                
                lis.suppress_event()
                return      
            if not cfg.g_is_acc==1  and cfg.jump_state==0 and ct-cfg.left_mouse_time>0.3 and \
                not cfg.can_set_acc:
                global sup_co
                sup_co+=1
                if sup_co%cfg.sup_ran==0:                
                    lis.suppress_event()
                return
            return
        
        if st in ('513','514'):
            if ig_left():
                return
            
            if not is_left_leftBtn():
                return
            if st=='513':
                req3_co1+=1                    
            else:
                req3_co2+=1                
            lis.suppress_event()                
            return
        
        if st in ('516','517'):
            if is_left_rightBtn():
                if st=='516':
                    req1_co1+=1
                else:
                    req1_co2+=1                
                lis.suppress_event()
                return
            if st=='516':
                on_click2(0,0,'',True,False)
            return
        
        if st=='519':
            cfg.x_press_time = time.time()
            cfg.ignMid = is_right()            
            if cfg.ignMid:
                cfg.middle_pressed = True
                cfg.middle_pressed_other = False
                req2_co1+=1
                lis.suppress_event()
            else:
                on_click2(0,0,'',True,False)

        if st=='520':
            if cfg.ignMid:
                cfg.middle_pressed = False
                req2_co2+=1
                lis.suppress_event()                                

        if pm.enter_pressed:
            return 

        if st=='522' and ( (cfg.w2showing and not cfg.isShow ) or (cfg.min_ge!=0 and is_no_move())):
            lis.suppress_event()
            return
        xx = cfg.jump_state!=0 or is_acc_up() or time.time()-cfg.left_mouse_time<0.2 or cfg.jump_kuai!=0
        xx2 = cfg.jump_state==1 and not cfg.x2_mode and \
        (cfg.jump_from_click or not cfg.can_break)
        leftMan = xx2

        if st=='522'  and  (cfg.middle_pressed or cfg.x2_pressed or cfg.ctrl2_job):
            lis.suppress_event()
            return
        if st=='522' and is_left_sc() and (xx and not leftMan):
            lis.suppress_event()
            return
        if  st=='522' and is_left_sc() and cfg.jump_state==0:
            lis.suppress_event()
            return
            
    lis = mouse.Listener(win32_event_filter=win32_event_filter)
    lis.start()

    thrb = threading.Thread(target=read_mouse_q_thr)
    thrb.daemon = True
    thrb.start()
    
    mouse_listener = mouse.Listener(on_move=on_move,on_click=on_click)
    mouse_listener.start()        
    moveT = threading.Thread(target=moveF)
    moveT.daemon = True
    moveT.start()
    cpuT = threading.Thread(target=writeThr)
    cpuT.daemon = True
    cpuT.start()

    load_pos()
    listener = keyboard.Listener(
        on_release=on_release,
        on_press=on_press,
    )
    
    listener.start()        
    app = QApplication([])   
    w2 = my77()
    w3 = my3()
    w4 = my4()
    q = mp.Queue()    
    p = mp.Process(target=pro_input, args=(q,))
    p.daemon = True
    p.start()
    print('pid', os.getpid(), p.pid)        

    def chk_p_thr():
        p.join()
        cfg.to_exit = True
    chpT = threading.Thread(target=chk_p_thr)
    chpT.daemon = True
    chpT.start()    
                
    tt = threading.Thread(target=pro_get,args=(q,))
    tt.daemon = True
    tt.start()
        
    app.exec_()        